package com.wanlian.service.impl;

import com.wanlian.common.PageCondition;
import com.wanlian.dao.OrgRoleGroupDao;
import com.wanlian.entity.OrgRoleGroup;
import com.wanlian.entity.RoleGroupAuthority;
import com.wanlian.entity.RoleGroupUser;
import com.wanlian.model.ResultModel;
import com.wanlian.service.OrgRoleGroupService;
import com.wanlian.service.RoleGroupAuthorityService;
import com.wanlian.service.RoleGroupUserService;
import com.wanlian.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class OrgRoleGroupServiceImpl implements OrgRoleGroupService
{

    @Autowired
    private OrgRoleGroupDao orgRoleGroupDao;


    @Override
    public void add(OrgRoleGroup group)
    {
        orgRoleGroupDao.add(group);
    }

    @Override
    public void doedit(OrgRoleGroup group)
    {
        orgRoleGroupDao.doedit(group);
    }



    @Override
    public ResultModel queryPageList(PageCondition pageCondition){
        return orgRoleGroupDao.queryPageList(pageCondition);
    }


    @Override
    public ResultModel add(OrgRoleGroup orgRoleGroup, int userId) {
        ResultModel resultModel = new ResultModel(true);
        if (orgRoleGroupDao.queryByName(orgRoleGroup.getRolegroupname()) != null) {
            resultModel.setBresult(false);
            resultModel.setMessage("该权限组名称已存在");
            return resultModel;
        }
        int parentId=orgRoleGroup.getParentid();
        if(orgRoleGroupDao.queryById(parentId)==null){
            resultModel.setBresult(false);
            resultModel.setMessage("添加失败，父级节点不存在");
            return resultModel;
        }
        orgRoleGroup.setCreateId(userId);
        orgRoleGroup.setCreatetime(DateUtil.currentTimestamp());
        orgRoleGroupDao.add(orgRoleGroup);
        resultModel.setMessage("添加成功");
        return resultModel;
    }


    @Override
    public ResultModel doEdit(OrgRoleGroup orgRoleGroup, int userId) {
        ResultModel resultModel = new ResultModel(true);
        OrgRoleGroup db_orgRoleGroup=orgRoleGroupDao.queryById(orgRoleGroup.getRoleGroupId());
        db_orgRoleGroup.setRolegroupname(orgRoleGroup.getRolegroupname());
        db_orgRoleGroup.setCreateId(userId);
        db_orgRoleGroup.setLastupdatetime(DateUtil.currentTimestamp());
        orgRoleGroupDao.update(db_orgRoleGroup);
        resultModel.setMessage("修改成功");
        return resultModel;
    }

    @Override
    public ResultModel delete(int id) {
        ResultModel resultModel = new ResultModel(true);
        List<OrgRoleGroup> list = orgRoleGroupDao.queryByPid(id);
        if (list.size() > 0) {
            resultModel.setMessage("禁用失败，该用户组下还有分组");
            resultModel.setBresult(false);
        } else {
            OrgRoleGroup orgRoleGroup = orgRoleGroupDao.queryById(id);
            orgRoleGroup.setIsenable(false);
            orgRoleGroupDao.update(orgRoleGroup);
            resultModel.setMessage("删除成功");
        }
        return resultModel;
    }

    @Override
    public ResultModel queryGroup(OrgRoleGroup orgRoleGroup) {
        List<Map<String, Object>> list = orgRoleGroupDao.queryGroup(orgRoleGroup);
        ResultModel resultModel = new ResultModel(true, list);
        return resultModel;
    }
}
